package com.erju.service.impl;

import com.erju.dao.EquipmentMapper;
import com.erju.exception.MyRuntimeException;
import com.erju.pojo.EquipApplication;
import com.erju.pojo.EquipRepair;
import com.erju.pojo.Equipment;
import com.erju.pojo.User;
import com.erju.pojo.VO.PageAll;
import com.erju.service.EquipmentService;
import com.erju.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author sunyuan
 * @date 2022/4/6 18:26
 */
@Service
public class EquipmentServiceImpl implements EquipmentService {


    Logger logger = LoggerFactory.getLogger(EquipmentServiceImpl.class);

    @Autowired
    EquipmentMapper equipmentMapper;

    /**
     * 查询所有设备
     *
     * @return
     */
    @Override
    public Result selectAllEquipment(HashMap<String, String> map) {

        logger.info("selectAllEquipment(HashMap<String, String> map)开始-----------------");
        //分页条件
        String pageNum = map.get("pageNum");
        String pageSize = map.get("pageSize");
        //模糊查询条件
        Equipment equipment = new Equipment();
        String equipment_name = map.get("equipment_name");
        equipment.setEquipment_name(equipment_name == null ? "" : equipment_name);
        // pageNum:表示第几页  pageSize:表示一页展示的数据
        PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        List<Equipment> equipments = null;
        try {
            //查询所有设备
            equipments = equipmentMapper.selectAllEquipment(equipment);
            // 将查询到的数据封装到PageInfo对象
            PageInfo<Equipment> equipmentPageInfo = new PageInfo(equipments, Integer.parseInt(pageSize));
            logger.info("selectAllEquipment(HashMap<String, String> map)结束-----------------");
            // 分割数据成功
            return Result.success("查询成功", equipmentPageInfo);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            // 将查询到的数据封装到PageInfo对象
            PageInfo<Equipment> equipmentPageInfo = new PageInfo(null, Integer.parseInt(pageSize));
            // 分割数据成功
            return Result.error(e.getMessage());
        }
    }

    /**
     * 我要申请
     * 业务逻辑
     * 根据商品的id或名字查询 空闲容量 合法的话更新库存  更新申请表
     *
     * @param equipApplication
     * @return
     */
    @Override
    public Result applicationEquipment(EquipApplication equipApplication) {
//        String equipment_name = equipApplication.getEquipment_name();
        Integer equipment_id = equipApplication.getEquipment_id();
        //根据商品id
        Equipment equipment = equipmentMapper.selectEquipmentById(equipment_id);
        //申请数量
        Integer application_number = equipApplication.getApplication_number();

        if (application_number > equipment.getFree_number()) {
            //预约数量 > 空闲数量
            return Result.error("申请数量大于空闲数量");
        }
        //更新库存
        equipment.setFree_number(equipment.getFree_number() - application_number);
        int flag = equipmentMapper.updateEquipment(equipment);
        if (flag == 1) {
            //更新预约表
            if (equipmentMapper.insertEquipmentApplication(equipApplication) > 0) {
                return Result.success("申请成功，请等待管理员同意");
            }
        } else {
            //修改库存失败
            return Result.error("1", "修改库存失败");
        }
        return Result.error("1", "失败");
    }

    @Override
    public Result selectAllRepairs(PageAll pageAll) {
        Integer pageNum = pageAll.getPageNum();
        Integer pageSize = pageAll.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<EquipRepair> lists = null;
        try {
            lists = equipmentMapper.selectAllRepairs();
            PageInfo<EquipRepair> pageInfo = new PageInfo(lists, pageSize);
            return Result.success("查询成功", pageInfo);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            PageInfo<EquipRepair> pageInfo = new PageInfo(null, pageSize);
            // 分割数据成功
            return Result.error(e.getMessage());
        }
    }

    /**
     * 管理员处理报修
     *
     * @param equipRepair
     * @return
     */
    @Override
    public Result handleRepairs(EquipRepair equipRepair) {
        equipRepair.setRepair_status(1);
        int i = equipmentMapper.updateHandleRepairs(equipRepair);
        if (i > 0) {
            return Result.success("处理成功");
        } else {
            return Result.error("处理失败，请检查参数是否正确");
        }
    }

    @Override
    public Result toReturnBack(EquipApplication equipApplication) {
        equipApplication.setIs_back(1);

        int flag = equipmentMapper.updateReturnBack(equipApplication);
        if (flag > 0) {
            return Result.success("归还成功");
        } else {
            return Result.error("提交失败，请检查参数后重新提交");
        }
    }

    /**
     * 报修
     *
     * @param equipRepair
     * @return
     */
    @Override
    public Result reportRepairs(EquipRepair equipRepair) {
        int flag = equipmentMapper.reportRepairs(equipRepair);
        if (flag > 0) {
            return Result.success("报修成功，请等待处理");
        } else {
            return Result.error("填写失败，请检查后重新提交");
        }
    }

    /**
     * 查询所有预约成功的产品
     *
     * @param pageAll
     * @return
     */
    @Override
    public Result selectAgreeEquipApplicationInfo(PageAll pageAll) {

        Integer pageNum = pageAll.getPageNum();
        Integer pageSize = pageAll.getPageSize();
        String username = pageAll.getUsername();
        PageHelper.startPage(pageNum, pageSize);
        List<EquipApplication> lists = null;
        try {
            lists = equipmentMapper.selectAgreeEquipApplicationInfo(username);
            PageInfo<EquipApplication> pageInfo = new PageInfo(lists, pageSize);
            return Result.success("查询成功", pageInfo);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            PageInfo<EquipApplication> pageInfo = new PageInfo(null, pageSize);
            // 分割数据成功
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询所有预约信息列表
     *
     * @param pageAll
     * @return
     */
    @Override
    public Result selectEquipApplicationInfo(PageAll pageAll) {
        Integer pageNum = pageAll.getPageNum();
        Integer pageSize = pageAll.getPageSize();
        String username = pageAll.getUsername();
        PageHelper.startPage(pageNum, pageSize);
        List<EquipApplication> lists = null;
        try {
            lists = equipmentMapper.selectEquipApplicationInfo(username);
            PageInfo<EquipApplication> pageInfo = new PageInfo(lists, pageSize);
            return Result.success("查询成功", pageInfo);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);

            PageInfo<EquipApplication> pageInfo = new PageInfo(null, pageSize);
            // 分割数据成功
            return Result.error(e.getMessage());
        }

    }

    /**
     * 查询所有设备
     *
     * @return
     */
    @Override
    public Result selectAllApplication(HashMap<String, String> map) {
        logger.info("selectAllApplication()开始");

        //分页条件
        String pageNum = map.get("pageNum");
        String pageSize = map.get("pageSize");

        PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        List<EquipApplication> equipApplications = null;


        try {
            //查询所有设备
            equipApplications = equipmentMapper.selectAllApplication();
            // 将查询到的数据封装到PageInfo对象
            PageInfo<EquipApplication> equipmentApplicationPageInfo = new PageInfo(equipApplications, Integer.parseInt(pageSize));
            // 分割数据成功
            return Result.success("查询成功", equipmentApplicationPageInfo);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            // 将查询到的数据封装到PageInfo对象
            PageInfo<EquipApplication> equipmentApplicationPageInfo = new PageInfo(null, Integer.parseInt(pageSize));
            // 返回失败
            return Result.error(e.getMessage());
        }
    }

    /**
     * 处理申请列表
     *
     * @param equipApplication
     * @return
     */
    @Override
    public Result toHandleApplication(EquipApplication equipApplication) {
        logger.info("toHandleApplication(EquipApplication equipApplication)");
        Integer statusArgree = 1;
        Integer application_status = equipApplication.getApplication_status();
        if (statusArgree.equals(application_status)) {
            equipApplication.setReason("同意~");
        }
        //更新数据库
        int flag = equipmentMapper.updateApplication(equipApplication);
        if (flag > 0) {
            return Result.success("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }


}
